<?php 
class CategoryModel extends Model{
	public $table = 'category';
	public $validate = array(
		array('cname','nonull','类型名不能为空',2,3),
	);
	public function addData(){
		if(!$this->create())return FALSE;
	    return $this->add();
	}
	public function delData($id){
		//		判断是否有子分类
		if($this->where("pid=$id")->all()){
			$this->error = "有子分类存在，请先删除子分类！";
			return FALSE;
		}
	    return $this->where("cid=$id")->delete();
	}
	public function delallData($id){
		$data = $this->getSon($this->all(), $id);
		$data[] = $id;
		foreach ($data as $v) {
			$this->where("cid=$v")->delete();
		}
	    return TRUE;
	}
	public function upData($id){
		if(!$this->create())return FALSE;
		return $this->where("cid=$id")->update();
	}
	public function getSon($data,$cid){
	    $temp = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->getSon($data,$v['cid']));
			}
		}
		return $temp;
	}
}
